package top.lucky.kafka.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import top.lucky.kafka.utils.PropertiesUtil;

import java.util.Properties;

/**
 * @author Jane
 * @date 2022/12/25, 16:39
 */
public class ProducerParameters {
	public static void main(String[] args) {
		Properties properties = PropertiesUtil.getProperties();
		
		//缓冲区大小
		properties.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
		//批次大小
		properties.put(ProducerConfig.BATCH_SIZE_CONFIG, 16384);
		//linger.ms
		properties.put(ProducerConfig.LINGER_MS_CONFIG, 1);
		//压缩
		properties.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, "snappy");
		
		//设置ack
		properties.put(ProducerConfig.ACKS_CONFIG, "1");
		//重试次数
		properties.put(ProducerConfig.RETRIES_CONFIG, "3");
		//创建生产者
		KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String, String>(properties);
		//发送数据
		for (int i = 0; i < 500 ; i++) {
			
			kafkaProducer.send(new ProducerRecord<>("first", "你好" + i));
		}
		//关闭连接
		kafkaProducer.close();
	}
}
